package com.example.smart_city_0829.activity.digitallibrary;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.digitalLibrary.LibraryList;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeLv;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;
import retrofit2.Response;

public class DigitalLibraryActivity extends BaseActivity {

    private MaxSizeLv lv;
    private Button load;

    private List<LibraryList.RowsBean> libraries = new ArrayList<>();
    private LibraryListAdapter adapter;

    private int pageNum = 1;
    private final int pageSize = 10;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_digital_library;
    }

    @Override
    protected void initView() {
        lv = (MaxSizeLv) findViewById(R.id.lv);
        load = (Button) findViewById(R.id.load);
    }

    @Override
    protected void initData() {
        getLibraryList(pageNum, pageSize);
    }

    @Override
    protected void initEvent() {
        load.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getLibraryList(++pageNum, pageSize);
            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final Intent intent = new Intent(DigitalLibraryActivity.this, DigitalLibraryDetailActivity.class);
                intent.putExtra("id",libraries.get(position).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "数字图书馆";
    }

    private void getLibraryList(int pageNum, int pageSize) {
        api.getDigitLibraryList(
                        pageNum,
                        pageSize)
                .enqueue(new NetCallBack<LibraryList>() {
            @Override
            public void onResponse(Call<LibraryList> call, Response<LibraryList> response) {
                showToastShort(response.body().getMsg());
                final List<LibraryList.RowsBean> newData = response.body().getRows();
                if (newData == null) return;
                libraries.addAll(newData);
                //优先展示营业状态的图书馆
                libraries = libraries.stream()
                        .sorted(Comparator.comparing(LibraryList.RowsBean::getBusinessState).reversed())
                        .collect(Collectors.toList());

                if (adapter == null) {
                    adapter = new LibraryListAdapter(libraries, DigitalLibraryActivity.this);
                    lv.setAdapter(adapter);
                } else {
                    adapter.resetData(libraries);
                    adapter.notifyDataSetChanged();
                }
                Log.e("test",libraries.size()+"");
            }
        });
    }

    public static final class LibraryListAdapter extends CommonAdapter<LibraryList.RowsBean, NewsListViewHolder> {

        public LibraryListAdapter(List<LibraryList.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(LibraryList.RowsBean data, int position, NewsListViewHolder viewHolder, View convertView, Context context) {
            viewHolder.im.setVisibility(View.VISIBLE);
            Glide.with(convertView.getContext())
                    .load(Constant.BASE_URL + data.getImgUrl())
                    .into(viewHolder.im);
            viewHolder.title.setText(String.valueOf(data.getName()));
            viewHolder.content.setText("图书馆地址:" + data.getAddress() + "\n" + "营业状态:" + (data.getBusinessState() == 0 ? "暂停营业" : "正在营业"));
            viewHolder.content.append("\n"+"营业时间：" + data.getBusinessHours());
            viewHolder.tag2.setVisibility(View.GONE);
        }

        @Override
        protected NewsListViewHolder getViewHolder() {
            return new NewsListViewHolder();
        }

        @Override
        protected void initView(NewsListViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
            viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
            viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
        }
    }

    private static class NewsListViewHolder {
        ImageView im;
        TextView title;
        TextView content;
        TextView tag1;
        TextView tag2;
        TextView tag3;
    }
}